﻿using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using FarmSmartSleep.Common.Domain.PM;
using static FarmSmartSleep.Organ.API.Application.PromptReportCommands.PendingSelectHandler;

namespace FarmSmartSleep.Organ.API.Application.PromptReportCommands
{
    public class ChildbirthTipsSelectHandler : IRequestHandler<ChildbirthTipsSelectCommand, Result>
    {
        private readonly IBaseRepository<SleepInfor> sleepinforRep;
        private readonly IBaseRepository<Cottages> cottagesRep;
        private readonly IBaseRepository<Fields> fieldsRep;
        private readonly IBaseRepository<MotherHybridization> eweMatingRep;

        public ChildbirthTipsSelectHandler(IBaseRepository<SleepInfor> sleepinforRep, IBaseRepository<Cottages> cottagesRep, IBaseRepository<Fields> fieldsRep,IBaseRepository<MotherHybridization> eweMatingRep)
        {
            this.sleepinforRep = sleepinforRep;
            this.cottagesRep = cottagesRep;
            this.fieldsRep = fieldsRep;
            this.eweMatingRep = eweMatingRep;
        }


        /// <summary>
        /// 分娩提示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(ChildbirthTipsSelectCommand request, CancellationToken cancellationToken)
        {
            var quest = new Result() { Code = (int)CodeEnum.success };

            var list = (from a in sleepinforRep.GetAll()
                        join b in cottagesRep.GetAll() on a.RecordcottageId equals b.CottagesId
                        join c in fieldsRep.GetAll() on a.FieIdId equals c.FieIdId
                        join d in eweMatingRep.GetAll() on a.SleepInforId equals d.MotherSleepInforId
                        select new PendingDTO()
                        {
                            SleepInforId = a.SleepInforId,
                            MoEarNumber = a.MoEarNumber,
                            CottagesId = b.CottagesId,
                            CottageName = b.CottageName,
                            FieIdId = c.FieIdId,
                            FieIdName = c.FieIdName,
                            BreedStatuId = a.BreedStatuId,
                            SleepEntryDate = a.SleepEntryDate,
                            BreedingDate = d.CreateTime,
                        });

            quest.Code = (int)CodeEnum.success;
            quest.Data = list;

            return quest;
        }
    }
}
